#include "main.h"

uint8_t data_leida[6]={0x12,0X00,0X00,0X00,0X00,0X13};   //角度，距离，速度
msg_data_t mag_Send_data10= 
{
    .value = data_leida,                // 指向预分配内存
    .value_len = sizeof(data_leida)     // 明确数据长度
};

uint8_t buffer8[128] = {0x10};  //MPU6050
uint8_t buffer1[128] = {"sss"};  //呼叫紧急联系人
msg_data_t mag_Send_data8= 
{
    .value = buffer8,                // 指向预分配内存
    .value_len = sizeof(buffer8)     // 明确数据长度
};
 msg_data_t mag_Send_data7= 
{
    .value = buffer1,                // 指向预分配内存
    .value_len = sizeof(buffer1)     // 明确数据长度
};  

typedef enum {
    AX = 0,
    AY,
    AZ,
    GX,
    GY,
    GZ,
    COUNT  // 数组长度
} imu_axis_t;

typedef struct {
    int16_t data[COUNT];
} imu_data_t;

imu_data_t imu;

void uart2_read_handler(const void *buffer, uint16_t length, bool error);
unsigned long g_msg_queue = 0;
unsigned int g_msg_rev_size = sizeof(msg_data_t);

static void *sle_server_task(const char *arg)
{
    unused(arg);
    osal_msleep(50);  //500: 延时50ms 
    sle_server_init();
    uart_gpio_init();
    uart_init_config();
    while (1) {
                msg_data_t msg_data = {0};
                int msg_ret = osal_msg_queue_read_copy(g_msg_queue, &msg_data, &g_msg_rev_size, OSAL_WAIT_FOREVER);
                if (msg_ret != OSAL_SUCCESS) {
                     printf("msg queue read copy fail.");
                    if (msg_data.value != NULL) {
                        osal_vfree(msg_data.value);
                      }
        }
        if (msg_data.value != NULL) {
            sle_server_send_report_by_handle(msg_data);
        }
            osDelay(20);
    }

    return NULL;
}

static void*MPU6050_task(const char *arg)
{
    unused(arg);    
    MPU6050_Init();
    
    while(1)
    {

    MPU6050_GetData(
    &imu.data[AX],
    &imu.data[AY],
    &imu.data[AZ],
    &imu.data[GX],
    &imu.data[GY],
    &imu.data[GZ]
    );//获取MPU6050的数据
    osDelay(50);
    // printf("AX = %d    AY = %d    AZ = %d  GX = %d    GY = %d    GZ = %d\r\n", 
    //imu.data[AX],imu.data[AY], imu.data[AZ],imu.data[GX],imu.data[GY],imu.data[GZ]);
    int i=imu.data[AX];
    if(i>800||i<-800)
    {
        msg_data_t msg_data1={0};
        msg_data1=mag_Send_data8;
        osal_msg_queue_write_copy(g_msg_queue, &msg_data1, sizeof(msg_data1), 0);
    }


    }
    return NULL;
}

static void*leida_task(const char *arg)
{
    unused(arg);   
    while(1)
    {
      if(leida_flag==2)
      {
        if((195<data_leida[1])&&(data_leida[1]<256))//-1~-60°
        {
            printf("zuoce\n");
           // osal_msg_queue_write_copy(g_msg_queue, &msg_data5, sizeof(msg_data5), 0);
            sle_server_send_report_by_handle(mag_Send_data10);
            osDelay(20);
            leida_flag=0;
        }

      }
      if(leida_flag==1)
      {
        if((196>data_leida[1])&&(data_leida[1]>0))
        {
            printf("youce\n");
           // osal_msg_queue_write_copy(g_msg_queue, &msg_data5, sizeof(msg_data5), 0);
            sle_server_send_report_by_handle(mag_Send_data10);
            osDelay(20);
            leida_flag=0;
        }
      }
     osDelay(20);
    }
    return NULL;
}

static void main(void)
{
    
    osal_task *task_handle = NULL;
    osal_task *task1_handle = NULL;
    osal_task *task2_handle = NULL;
    osal_kthread_lock();
    int ret = osal_msg_queue_create("sle_msg", g_msg_rev_size, &g_msg_queue, 0, g_msg_rev_size);
    if (ret != OSAL_SUCCESS) {
        printf("create queue failure!,error:%x\n", ret);
    }

    task1_handle =osal_kthread_create((osal_kthread_handler)MPU6050_task, 0, "MPU6050_task", SLE_SERVER_STACK_SIZE);
    task_handle  =osal_kthread_create((osal_kthread_handler)sle_server_task, 0, "sle_server_task", SLE_SERVER_STACK_SIZE);
    task2_handle =osal_kthread_create((osal_kthread_handler)leida_task, 0, "leida_task", SLE_SERVER_STACK_SIZE);
        
    if (task_handle != NULL) {
        osal_kthread_set_priority(task_handle, SLE_SERVER_TASK_PRIO);
        osal_kfree(task_handle);
    }

    if (task1_handle != NULL) {
        printf("task1 is success");
        osal_kthread_set_priority(task1_handle, 25);
        osal_kfree(task1_handle);
    }

    if (task2_handle != NULL) {
        printf("task2 is success");
        osal_kthread_set_priority(task2_handle, 25);
        osal_kfree(task2_handle);
    }
    osal_kthread_unlock();
}

/* Run the sle_entry. */
app_run(main);
void uart2_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);
    if (buffer == NULL || length == 0) {
        return;
    }

    // 动态分配内存
    int8_t *buffer_cpy = osal_vmalloc(length);

    if (buffer_cpy == NULL) {
        printf("Failed to allocate memory for buffer_cpy");
        return;
    }
    // 复制数据
    if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) {
        osal_vfree(buffer_cpy);
        return;
    }

    // 打印数据b
  /*  for (int i = 0; i < length; i++) {
        printf("%02X ", buffer_cpy[i]);
    }
    printf("\r");*/
    if(length>10)
    { 
        data_leida[1]=(buffer_cpy[8]-128);
        data_leida[2]=buffer_cpy[9];
        data_leida[3]=buffer_cpy[11];
        printf("%d\n",data_leida[1]);
        printf("%d\n",data_leida[2]);
        printf("%d\n",data_leida[3]);   
    }
}
/*
void uart0_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);
    if (buffer == NULL || length == 0) {
        return;
    }

    // 动态分配内存
    int8_t *buffer_cpy = osal_vmalloc(length);
    if (buffer_cpy == NULL) {
        printf("Failed to allocate memory for buffer_cpy");
        return;
    }
    // 复制数据
    if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) {
        osal_vfree(buffer_cpy);
        return;
    }
        msg_data_t    msg_data={0};
        msg_data.value =buffer_cpy;  // 指向动态内存
        msg_data.value_len = sizeof(buffer_cpy);
        osal_msg_queue_write_copy(g_msg_queue, &msg_data, sizeof(msg_data), 0); 
}
*/
